<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="begin">开启定时器</button>
    <button class="stop">关闭定时器 </button>

    <script>
        var begin = document.querySelector('.begin');
        var stopa = document.querySelector('.stop');
        // begin.onclick = function () {
        //     setInterval(function () {
        //         console.log('111');
        //     }, 1000)
        // }
        var times = null;
        begin.addEventListener('click', function () {
            times = setInterval(function () {
                console.log('haha');
            }, 1000)

        })
        stopa.addEventListener('click', function () {
            clearInterval(times);
        })


    </script>
</body>

</html>